using System;
using System.IO;
using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;

namespace FireStarter.CodeGeneration
{
    public class VWGProjectTemplateLoader : TemplateLoader
    {
        public string GetUiManagerVWG(string namespaceName, string uiBaseProjectName, string uiVWGProjectName, string applicationName)
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_UIManagerVWG_cs;
            }
            SetNamespaceTag(ref fileContents, namespaceName);
            fileContents = fileContents.Replace("<<UIBASEPROJECT_NAME>>", uiBaseProjectName);
            fileContents = fileContents.Replace("<<UIVWGPROJECT_NAME>>", uiVWGProjectName);
            fileContents = fileContents.Replace("<<APPLICATION_NAME>>", applicationName);
            return fileContents;
        }

        public string GetWebConfig(DMSolution modelSolution)
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_Web_config;
            }
            DatabaseConnectionInfo databaseConnectionInfo = modelSolution.GenerationSettings.DatabaseConnection;
            if (databaseConnectionInfo == null)
            {
                databaseConnectionInfo = new DatabaseConnectionInfo();
                databaseConnectionInfo.Vendor = "InMemory";
            }

            databaseConnectionInfo.Parent = modelSolution;
            return GetGeneratedTemplate(fileContents, databaseConnectionInfo);
        }


        public string GetMainForm(string namespaceName)
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_Mainform_cs;
            }
            SetNamespaceTag(ref fileContents, namespaceName);
            return fileContents;
        }

        public string GetGlobal_asax(string namespaceName)
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_Global_asax;
            }
            SetNamespaceTag(ref fileContents, namespaceName);
            return fileContents;
        }

        public string GetGlobal_asax_cs(string namespaceName, string projectName, string projectVersion, string applicationName, string boProjectName)
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_Global_asax_cs;
            }
            if (String.IsNullOrEmpty(projectVersion))
            {
                projectVersion = "v1.0";
            }
            SetNamespaceTag(ref fileContents, namespaceName);
            fileContents = fileContents.Replace("<<PROJECT_NAME>>", projectName);
            fileContents = fileContents.Replace("<<PROJECT_VERSION>>", projectVersion);
            fileContents = fileContents.Replace("<<APPLICATION_NAME>>", applicationName);
            fileContents = fileContents.Replace("<<BOPROJECT_NAME>>", boProjectName);
            return fileContents;
        }

        public string GetVWGCSProjUser()
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_csproj_user;
            }
            return fileContents;
        }

        public string GetMainFormForVWGOnly(string namespaceName, string projectName, string projectVersion, string applicationName, string uiProjectName)
        {
            string fileName = "";
            string fileContents;
            if (!loadFromFile(fileName, out fileContents))
            {
                fileContents = Templates.VWG_MainformVWGOnly_cs;
            }
            if (String.IsNullOrEmpty(projectVersion))
            {
                projectVersion = "v1.0";
            }
            SetNamespaceTag(ref fileContents, namespaceName);
            fileContents = fileContents.Replace("<<PROJECT_NAME>>", projectName);
            fileContents = fileContents.Replace("<<PROJECT_VERSION>>", projectVersion);
            fileContents = fileContents.Replace("<<APPLICATION_NAME>>", applicationName);
            fileContents = fileContents.Replace("<<UIPROJECT_NAME>>", applicationName);
            return fileContents;
        }
    }
}